//引入模块
import axios from 'axios'

// 引入nprogress
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'
import { getUserTemped } from '@/utils/getUserTempId'
import store from '@/store'

//创建实例对象
const ajax = axios.create({
    baseURL: '/api',
    timeout: 20000,
})

//设置请求拦截器
ajax.interceptors.request.use(config => {
    // console.log('打印config',config);
    NProgress.start()
    config.headers.userTempId = getUserTemped()
    let token = store.state.user.token
    if (token) {
        config.headers.token = token
    }

    // 判断一下，如果没有token则要重新赋值一个token

    return config
})

//设置响应拦截器
ajax.interceptors.response.use(result => {
    NProgress.done()
    return result.data
}, err => {
    // console.log(Promise.reject(new Error(err)));
    return Promise.reject(err)
})

export default ajax